/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: userSystem.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月19日 星期五 00时28分07秒
***************************************************/

#include "userSystem.h"


//会员系统函数
void userSystem(void){
	//会员登录信息变量
	UL *head_ul = NULL;
	FILE *fp_ul = NULL;
	
	int model = 0;
	int n = 0;

	logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model info]:%s",\
			__FILE__,__FUNCTION__,__LINE__,strerror(errno),"会员系统");
	system("clear");
	while(1){
		printf("---会员系统---\n");
		printf("1--会员登录\n");
		printf("2--会员修改密码\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"0--退出");
			system("clear");
			break;
		}
		switch(model){
			//会员登录
			case 1:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"1--会员登录");
				n = loginUser(fp_ul,head_ul);
				userOperateSystem(n);
				break;
			//会员修改密码
			case 2:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"2--会员修改密码");
				amendUser(fp_ul,head_ul);
				break;
			default:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"输入错误！");
				printf("输入错误！\n");
				break;
		}
	}
}


//会员登录函数
int loginUser(FILE *fp,UL *head){
	int n = 0;
	fp = fopen("./userLogin.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createUserLoginHead();
	head = readUserLoginFileToLink(fp,head);
	n = loginUserSystem(head);

	freeUserLoginLink(head);
	fclose(fp);
	printf("登录完成！\n");

	return n;
}

//会员修改密码函数
void amendUser(FILE *fp,UL *head){
	fp = fopen("./userLogin.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createUserLoginHead();
	head = readUserLoginFileToLink(fp,head);
	fclose(fp);
	head = amendUserLoginLink(head);
	fp = fopen("./userLogin.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	fp = saveUserLoginLinkToFile(fp,head);
	freeUserLoginLink(head);
	fclose(fp);
	printf("会员修改密码成功！\n");
}


